﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using ControlLib;
using AbstractFramework.Display;
using AbstractFramework.LogicControl;
using AbstractFramework.Constant;

namespace ElevatorEmulator
{
    /// <summary>
    /// Interaction logic for Main.xaml
    /// 这是电梯仿真的主窗口
    /// </summary>
    public partial class Main : Window
    {
        bool initialized = false;
        public Main()
        {
            InitializeComponent();
            this.Activated += new EventHandler(Main_Activated);
        }

        void Main_Activated(object sender, EventArgs e)
        {
            if(!initialized)
            {
                throw new NotInitializedException("窗口还未初始化，先调用initialize方法");
            }
        }

        //初始化界面，输入楼层数和电梯数，返回生成的各个控件数组
        public void initialize(int floorNum, int elevatorNum,IElevator[] logicElevators,IButtonFactory buttonFactory, out IElevatorDisplay[] elevatorDisplays, 
            out IInsideIndicatorDisplay[] insideIndicatorDisplays, out IInsideButtonPanelDisplay[] insideButtonPanelDisplays,
            out IOutsideButtonPanelDisplay[] outsideButtonPanelDisplays, out IFloorIndicatorDisplay[] upFloorIndicatorDisplays, out IFloorIndicatorDisplay[] downFloorIndicatorDisplays)
        {
            //添加布局用的表格
            for (int i = 0; i < floorNum + 2; i++)
            {
                RowDefinition newRow = new RowDefinition();
                newRow.Height = new GridLength(100);
                gridRoot.RowDefinitions.Add(newRow);
            }

            for (int i = 0; i < elevatorNum + 2; i++)
            {
                ColumnDefinition newCol = new ColumnDefinition();
                if (i == elevatorNum + 1)
                {
                    newCol.Width = new GridLength(52);
                }
                else
                {
                    newCol.Width = new GridLength(102);
                }
                gridRoot.ColumnDefinitions.Add(newCol);
            }

            this.Height = Math.Min((floorNum + 2) * 100 + 40, SystemParameters.WorkArea.Height);
            this.Width = Math.Min((elevatorNum + 2) * 102-42,SystemParameters.WorkArea.Width);

            //生成电梯对象
            elevatorDisplays = new ElevatorDisplay[elevatorNum];
            for (int i = 0; i < elevatorNum; i++)
            {
                ElevatorDisplay newElevatorDisplay = new ElevatorDisplay();
                //newElevatorDisplay.initialize(floorNum,0, logicElevators[i]);
                newElevatorDisplay.initialize(floorNum, logicElevators[i]);
                newElevatorDisplay.VerticalAlignment = VerticalAlignment.Top;
                newElevatorDisplay.HorizontalAlignment = HorizontalAlignment.Left;
                gridRoot.Children.Add(newElevatorDisplay);
                newElevatorDisplay.SetValue(Grid.ColumnProperty, i);
                newElevatorDisplay.SetValue(Grid.RowSpanProperty, floorNum);
                elevatorDisplays[i] = newElevatorDisplay;
            }

            //生成内部显示器对象
            insideIndicatorDisplays = new InsideIndicatorDisplay[elevatorNum];
            for (int i = 0; i < elevatorNum; i++)
            {
                InsideIndicatorDisplay newInsideIndicatorDisplay = new InsideIndicatorDisplay();
                newInsideIndicatorDisplay.VerticalAlignment = VerticalAlignment.Top;
                newInsideIndicatorDisplay.HorizontalAlignment = HorizontalAlignment.Left;
                gridRoot.Children.Add(newInsideIndicatorDisplay);
                newInsideIndicatorDisplay.SetValue(Grid.RowProperty, floorNum);
                newInsideIndicatorDisplay.SetValue(Grid.ColumnProperty, i);
                insideIndicatorDisplays[i] = newInsideIndicatorDisplay;
            }

            //生成内部按钮面板对象
            insideButtonPanelDisplays = new InsideButtonPanelDisplay[elevatorNum];
            for (int i = 0; i < elevatorNum; i++)
            {
                InsideButtonPanelDisplay newInsideButtonPanelDisplay =  new InsideButtonPanelDisplay();
                newInsideButtonPanelDisplay.initialize(floorNum,logicElevators[i], buttonFactory, i, logicElevators[i].FbdFloor);
                newInsideButtonPanelDisplay.VerticalAlignment = VerticalAlignment.Top;
                newInsideButtonPanelDisplay.HorizontalAlignment = HorizontalAlignment.Left;
                gridRoot.Children.Add(newInsideButtonPanelDisplay);
                newInsideButtonPanelDisplay.SetValue(Grid.RowProperty, floorNum+1);
                newInsideButtonPanelDisplay.SetValue(Grid.ColumnProperty, i);
                insideButtonPanelDisplays[i] = newInsideButtonPanelDisplay;
            }

            //生成每一层的显示器对象（向上的）
            upFloorIndicatorDisplays = new FloorIndicatorDisplay[floorNum];
            for (int i = 0; i < floorNum; i++)
            {
                FloorIndicatorDisplay newFloorIndicatorDisplay = new FloorIndicatorDisplay();
                newFloorIndicatorDisplay.VerticalAlignment = VerticalAlignment.Top;
                newFloorIndicatorDisplay.HorizontalAlignment = HorizontalAlignment.Left;
                gridRoot.Children.Add(newFloorIndicatorDisplay);
                newFloorIndicatorDisplay.SetValue(Grid.RowProperty, floorNum-1-i);
                newFloorIndicatorDisplay.SetValue(Grid.ColumnProperty, elevatorNum);
                upFloorIndicatorDisplays[i] = newFloorIndicatorDisplay;
            }

            //生成每一层的显示器对象（向下的）
            downFloorIndicatorDisplays = new FloorIndicatorDisplay[floorNum];
            for (int i = 0; i < floorNum; i++)
            {
                FloorIndicatorDisplay newFloorIndicatorDisplay = new FloorIndicatorDisplay();
                newFloorIndicatorDisplay.VerticalAlignment = VerticalAlignment.Top;
                newFloorIndicatorDisplay.HorizontalAlignment = HorizontalAlignment.Left;
                newFloorIndicatorDisplay.Margin = new Thickness(0, 50, 0, 0);
                gridRoot.Children.Add(newFloorIndicatorDisplay);
                newFloorIndicatorDisplay.SetValue(Grid.RowProperty, floorNum - 1 - i);
                newFloorIndicatorDisplay.SetValue(Grid.ColumnProperty, elevatorNum);
                downFloorIndicatorDisplays[i] = newFloorIndicatorDisplay;
            }

            //生成每一层楼的按钮面板对象
            outsideButtonPanelDisplays = new OutsideButtonPanelDisplay[floorNum];
            for (int i = 0; i < floorNum; i++)
            {
                OutsideButtonPanelDisplay newOutsideButtonPanelDisplay = new OutsideButtonPanelDisplay();
                newOutsideButtonPanelDisplay.initialize(buttonFactory, i,i==floorNum-1,i==0);
                newOutsideButtonPanelDisplay.VerticalAlignment = VerticalAlignment.Top;
                newOutsideButtonPanelDisplay.HorizontalAlignment = HorizontalAlignment.Left;
                gridRoot.Children.Add(newOutsideButtonPanelDisplay);
                newOutsideButtonPanelDisplay.SetValue(Grid.RowProperty, floorNum - 1 - i);
                newOutsideButtonPanelDisplay.SetValue(Grid.ColumnProperty, elevatorNum + 1);
                outsideButtonPanelDisplays[i] = newOutsideButtonPanelDisplay;
            }

            //生成火警按钮
            FireButtonDisplay fireButton = new FireButtonDisplay();
            fireButton.initialize(buttonFactory, logicElevators);
            fireButton.VerticalAlignment = VerticalAlignment.Top;
            fireButton.HorizontalAlignment = HorizontalAlignment.Center;
            gridRoot.Children.Add(fireButton);
            fireButton.SetValue(Grid.RowProperty, floorNum);
            fireButton.SetValue(Grid.ColumnProperty, elevatorNum);

            //生成退出程序按钮
            ExitButtonDisplay exitBtn = new ExitButtonDisplay();
            exitBtn.Owner = this;
            exitBtn.VerticalAlignment = VerticalAlignment.Top;
            exitBtn.HorizontalAlignment = HorizontalAlignment.Center;
            gridRoot.Children.Add(exitBtn);
            exitBtn.SetValue(Grid.RowProperty, floorNum+1);
            exitBtn.SetValue(Grid.ColumnProperty, elevatorNum);

            //退出程序时写文件
            this.Closed += new EventHandler(Main_Closed);

            initialized = true;
        }

        void Main_Closed(object sender, EventArgs e)
        {
            string resultFile = SystemConstant.uniqueStatistic.saveStatResult();
            MessageBox.Show("仿真结果已经保存到文件：" + resultFile, "提示", MessageBoxButton.OK, MessageBoxImage.Information);
        }
    }
}
